package com.baobao.service;

import com.baobao.dao.TeacherDAO;
import com.baobao.domain.Address;
import com.baobao.domain.Teacher;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Teacher entities
 * 
 */

@Service("TeacherService")
@Transactional
public class TeacherServiceImpl implements TeacherService {

	/**
	 * DAO injected by Spring that manages Teacher entities
	 * 
	 */
	@Autowired
	private TeacherDAO teacherDAO;

	/**
	 * Instantiates a new TeacherServiceImpl.
	 *
	 */
	public TeacherServiceImpl() {
	}

	/**
	 * Delete an existing Teacher entity
	 * 
	 */
	@Transactional
	public void deleteTeacher(Teacher teacher) {
		teacherDAO.remove(teacher);
		teacherDAO.flush();
	}

	/**
	 */
	@Transactional
	public Teacher findTeacherByPrimaryKey(String phone) {
		return teacherDAO.findTeacherByPrimaryKey(phone);
	}
	
	@Transactional
	public Set<Teacher> findTeacherByBanjiId(String banjiId){
		return teacherDAO.findTeacherByBanjiId(banjiId);
	}

	/**
	 * Return all Teacher entity
	 * 
	 */
	@Transactional
	public List<Teacher> findAllTeachers(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Teacher>(teacherDAO.findAllTeachers(startResult, maxRows));
	}

	/**
	 * Return a count of all Teacher entity
	 * 
	 */
	@Transactional
	public Integer countTeachers() {
		return ((Long) teacherDAO.createQuerySingleResult("select count(o) from Teacher o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Teacher entity
	 * 
	 */
	@Transactional
	public void saveTeacher(Teacher teacher) {
		Teacher existingTeacher = teacherDAO.findTeacherByPrimaryKey(teacher.getPhone());

		if (existingTeacher != null) {
			if (existingTeacher != teacher) {
				existingTeacher.setPhone(teacher.getPhone());
				existingTeacher.setName(teacher.getName());
				existingTeacher.setAccount(teacher.getAccount());
				existingTeacher.setPassword(teacher.getPassword());
				existingTeacher.setBanjiId(teacher.getBanjiId());
				existingTeacher.setImgUrl(teacher.getImgUrl());
				existingTeacher.setCourseRole(teacher.getCourseRole());
			}
			teacher = teacherDAO.store(existingTeacher);
		} else {
			teacher = teacherDAO.store(teacher);
		}
		teacherDAO.flush();
	}

	/**
	 * Load an existing Teacher entity
	 * 
	 */
	@Transactional
	public Set<Teacher> loadTeachers() {
		return teacherDAO.findAllTeachers();
	}
}
